package com.preguicoso.client.estabelecimento;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.preguicoso.client.busca.BuscaService;
import com.preguicoso.client.busca.BuscaServiceAsync;
import com.preguicoso.client.cadastro.CadastroService;
import com.preguicoso.client.cadastro.CadastroServiceAsync;
import com.preguicoso.client.checkout.Checkout;
import com.preguicoso.client.editar.EditarService;
import com.preguicoso.client.editar.EditarServiceAsync;
import com.preguicoso.client.estabelecimento.cardapio.Cardapio;
import com.preguicoso.client.estabelecimento.cardapio.CardapioCategoria;
import com.preguicoso.client.estabelecimento.cardapio.CardapioService;
import com.preguicoso.client.estabelecimento.cardapio.CardapioServiceAsync;
import com.preguicoso.client.estabelecimento.carrinho.Carrinho;
import com.preguicoso.client.estabelecimento.carrinho.CarrinhoPedidosUI;
import com.preguicoso.client.estabelecimento.filtro.Filtro;
import com.preguicoso.client.telaInicial.lightBox;
import com.preguicoso.shared.RegistroErros;
import com.preguicoso.shared.entities.BannerBean;
import com.preguicoso.shared.entities.CategoriaBean;
import com.preguicoso.shared.entities.EstabelecimentoBean;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.event.dom.client.KeyPressEvent;

public class EstabelecimentoUI extends Composite {

	private static EstabelecimentoUIUiBinder uiBinder = GWT
			.create(EstabelecimentoUIUiBinder.class);
	@UiField
	HTMLPanel coluna1;
	@UiField
	HTMLPanel coluna2;
	@UiField
	HTMLPanel coluna3;
	@UiField
	HTMLPanel flash;
	
	
	@UiField
	HTMLPanel filtro;
//	@UiField
//	Label nome;
	@UiField
	Label endereco;
	@UiField
	Image logo;
	
	@UiField InlineLabel descricao;
	@UiField Image banner;
	@UiField HTMLPanel pagina;

	private Long estabelecimentoId;
	private Object currentObject;

	private final CadastroServiceAsync cadastroService = GWT
			.create(CadastroService.class);
	private final BuscaServiceAsync buscaService = GWT
			.create(BuscaService.class);
	private final EditarServiceAsync editarService = GWT
			.create(EditarService.class);
	private final CardapioServiceAsync cardapioService = GWT
			.create(CardapioService.class);

	interface EstabelecimentoUIUiBinder extends
			UiBinder<Widget, EstabelecimentoUI> {
	}
	
	ArrayList<CardapioCategoria> listaCategoria = new ArrayList<CardapioCategoria>();

	public EstabelecimentoUI(final Long id) {
		
		this.estabelecimentoId = id;
		this.initWidget(uiBinder.createAndBindUi(this));
		filtro.setVisible(false);
		final Carrinho carrinho = Carrinho.getInstance();
		EstabelecimentoUI.this.coluna3.add(carrinho);
		this.cadastroService.getEstabelecimento(id,
				new AsyncCallback<EstabelecimentoBean>() {
					@Override
					public void onSuccess(EstabelecimentoBean result) {
						//if(result.getId()==10)
							//flash.add(new HTML("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='960' height='218' id='flash/topo' align='middle' scale='noscale'><param name='allowScriptAccess' value='sameDomain'><param name='movie' value='http://www.pontodeluzdunas.com.br/flash/topo.swf'><param name='quality' value='high'><param name='menu' value='false'><param name='wmode' value='transparent'><embed src='http://www.pontodeluzdunas.com.br/flash/topo.swf' quality='high' menu='false' wmode='transparent' width='960' height='218' name='flash/topo' align='middle' allowscriptaccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' scale='noscale'></object>"));
						EstabelecimentoUI.this.pagina.setStyleName("cssStyle"+result.getId());
						if(Window.getClientWidth()<500){
							pagina.addStyleName("iphone");
						}
						EstabelecimentoUI.this.currentObject = result;
						EstabelecimentoUI.this.logo.setUrl(result.getLogoURL());
						EstabelecimentoUI.this.descricao.setText(result.getDescricao());
						EstabelecimentoUI.this.endereco.setText(result.getEndereco());
						if(Window.getClientWidth()<500){
							pagina.addStyleName("iphone");
							banner.setWidth("320px");
						}
						if(result.getBanners() !=null)
							for (BannerBean b : result.getBanners()) {
								banner.setUrl(b.getUrlImage());
							}
						else
							banner.setVisible(false);
						String token = History.getToken();
						if (!token.endsWith("/checkout")) {
							carrinho.clear();
							Cardapio cardapio = new Cardapio(id);
							EstabelecimentoUI.this.coluna2.add(cardapio);
							filtro.add(new Filtro(id));
						} else {
							coluna1.add(new Checkout(result));
						}
						
					}

					@Override
					public void onFailure(Throwable caught) {
						//Window.alert(RegistroErros.CONEXAO);
					}
				});
	}
	@UiHandler("logo")
	void onLogoClick(ClickEvent event) {
		History.newItem(History.getToken().replaceAll("/checkout", ""));
	}
	
	static public Long getIdEstabelecimento(){
		String token = History.getToken();
		String[] partes = token.split("/");
		return Long.parseLong(partes[1]);
	}
	
	
}